//
//  String+Regex.swift
//  weibo
//
//  Created by 张宏芝 on 15/9/16.
//  Copyright © 2015年 kama. All rights reserved.
//

import Foundation


extension String{
//    从当前字符串中,提取超文本连接的URL*地址
//    swift中提供了元组,可以允许返回多个数值
    func href()->(link:String,text:String)?{
        let pattern = "<a href=\"(.*?)\".*?>(.*?)</a>"
//        正则表达式
        let regex = try! NSRegularExpression(pattern: pattern, options: [NSRegularExpressionOptions.DotMatchesLineSeparators])
        guard let result = regex.firstMatchInString(self, options: [], range: NSRange(location: 0, length: self.characters.count)) else{
            return nil
        }
        let link = (self as NSString).substringWithRange(result.rangeAtIndex(1))
        let text = (self as NSString).substringWithRange(result.rangeAtIndex(2))
        return (link, text)
    }
}

























